package ch.nostromo.tiffanys.game.chess.pieces;

import java.util.ArrayList;

import ch.nostromo.tiffanys.game.board.Board;
import ch.nostromo.tiffanys.game.board.BoardException;
import ch.nostromo.tiffanys.game.board.BoardTools;
import ch.nostromo.tiffanys.game.move.MoveInput;

public class Pawn extends ChessPiece {

  public static ArrayList<MoveInput> getMoveList(Board board, int currentPosition, int owner) throws BoardException {

    ArrayList<MoveInput> result = new ArrayList<MoveInput>();

    int toField = 0;
    int direction = 1;

    if (owner == Board.OWNER_BLACK)
      direction = -1;

    // One step forward (including possible "pawn promotion")
    toField = currentPosition + (10 * direction);
    if (board.isEmptyField(toField)) {
      if (toField > 90 || toField < 29) {
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_KNIGHT));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_QUEEN));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_ROOK));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_BISHOP));
      } else {
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
      }
    }

    // Two steps forward
    int toField1 = currentPosition + (10 * direction);
    toField = currentPosition + (20 * direction);
    if (!board.isMovedField(currentPosition) && board.isEmptyField(toField1) && board.isEmptyField(toField)) {
      result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    // hit Moves ...
    toField = currentPosition + (11 * direction);
    if (board.isOpponentField(toField, owner)) {
      if (toField > 90 || toField < 29) {
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_KNIGHT));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_QUEEN));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_ROOK));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_BISHOP));
      } else {
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
      }
    }

    // also for enpassant
    if (toField == board.getBoardArray()[Board.IDX_ENPASSANT_FIELD]) {
      result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    // hit Moves to the other side
    toField = currentPosition + (9 * direction);
    if (board.isOpponentField(toField, owner)) {
      if (toField > 90 || toField < 29) {
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_KNIGHT));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_QUEEN));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_ROOK));
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField), Board.PIECE_BISHOP));
      } else {
        result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
      }

    }
    // also for enpassant
    if (toField == board.getBoardArray()[Board.IDX_ENPASSANT_FIELD]) {
      result.add(new MoveInput(BoardTools.fieldToCoord(currentPosition), BoardTools.fieldToCoord(toField)));
    }

    return result;

  }

}